Ruby on Rails - resource routing basic in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
- Routes are defined in config/routes.rb. They are often defined as a group of related routes, using the resources or resource methods.
- resources :users
Creates the following seven routes, all mapping to actions of UsersController:
- Action names are shown after the # in the to parameter above. Methods with those same names must be defined in app/controllers/users_controller.rb as follows:
- You can limit the actions that gets generated with only or except:
- You can view all the routes of your application at any given time by running:
- To see only the routes that map to a particular controller:
- You can search through routes using the -g option. This shows any route that partially matches the helper method name, the URL path or the HTTP verb:
- when running rails server in development mode, you can access a web page that shows all your routes with a search filter, matched in priority from top to bottom, at
/rails/info/routes. It will look like this:
Helper | HTTP Verb | Path | cONTROLLER#Action |
---|---|---|---|
Path / Url | [ Path Match ] | ||
users_path | GET | /users(.:format) | users#index |
POST | /users(.:format) | users#create | |
new_user_path | GET | /users/new(.:format) | users#new |
edit_user_path | GET | /users/:id/edit(.:format) | users#edit |
user_path | GET | /users/:id(.:format) | users#show |
PATCH | /users/:id(.:format) | users#update | |
PUT | /users/:id(.:format) | users#update | |
DELETE | /users/:id(.:format) | users#destroy |
- Routes can be declared available for only members (not collections) using the method resource instead of resources in routes.rb. With resource, an index route is not created by default, but only when explicitly asking for one like this: